package com.project.jdbc.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * 正则表达式工具类
 * */
public class RegexValidateUtils {
	/**
	 * 对一字符串进行正则验证 验证一个字符串中长度必须大于6位，同时必须包含至少一个字符和一个数值
	 * 
	 * @param str
	 *            待验证的字符串
	 * 
	 * @return 通过验证时返回null 如果验证出现问题，会返回对应的失败原因
	 * @throws Exception
	 */
	public static String validateString(String str) throws Exception {
		// 验证长度
		if (validateLength(str) == null) {
			// 验证字符元素
			return isContainNumber(str);
		} else {
			return validateLength(str);
		}

	}

	/**
	 * 验证content中必须同时存在数字和字母
	 * 
	 * @param content
	 * @return 通过验证时，返回null 否则返回对应提示
	 * @throws Exception
	 * 
	 */
	public static String isContainNumber(String content) throws Exception {
		String message = "必须包含数字和字母";
		String restring = "^.*(([a-zA-Z]+.*[0-9]+)|([0-9]+.*[a-zA-Z]+)).*$"; // 必须六位以上 包含数字和字母
		Pattern pattern = Pattern.compile(restring);
		if (pattern.matcher(content).matches()) {
			return null;
		}
		return message;
	}

	/**
	 * 验证content的长度必须大于等六位
	 * 
	 * @param content
	 * @return 通过验证时，返回null 否则返回对应提示
	 * @throws Exception
	 */
	public static String validateLength(String content) throws Exception {
		String message = "必须六位以上";
		String restring = "^.{6,}$";
		Pattern pattern = Pattern.compile(restring);
		if (pattern.matcher(content).matches()) {
			return null;
		}
		return message;
	}

	/**
	 * 如果字符串url是否以指定的数组的任一字符串结尾
	 * 
	 * @param url
	 *            指定字符串
	 * @param noFomat
	 *            格式集[".js",".html"] <<主要是用于文件格式的验证，必须以"."开头加后缀名 如： .js >>
	 * @return url不是以指定的数组的任一字符串结尾 返回 true 否则返回false
	 */
	public static boolean validateUrl(String url, String[] noFomat) {
		// 正则非
		String endStr = "";
		// 为空时返回true
		if (noFomat == null || noFomat.length - 0 == 0 || url == null) {
			return true;
		} else {
			for (int i = 0; i < noFomat.length; i++) {
				if (i - 0 == 0) {
					endStr = "((\\" + noFomat[i] + ")";
				} else {
					endStr = endStr + "|(\\" + noFomat[i] + ")";
				}

				if (i - noFomat.length + 1 == 0) {
					endStr = endStr + ")$";
				}
			}
		}
		// 定义正则
		String reg = "^.*";
		// 验证文件后缀
		// 其中参数意义：不区分大小写的,默认只有一个参数时是区分大小写
		java.util.regex.Pattern p = java.util.regex.Pattern.compile(reg
				+ endStr, java.util.regex.Pattern.CASE_INSENSITIVE);
		return !p.matcher(url).matches();
	}

	/**
	 * 验证指定的字符串是不是符点型数值
	 * 
	 * @param value
	 *            某指定的字符串
	 * @return 字符串符合数值格式时返回true,否则返回false
	 */
	public static boolean isFloat(String value) {
		String restring = "^-?(\\d)+((.\\d+)|(\\d)*)$";
		Pattern pattern = Pattern.compile(restring);
		if (pattern.matcher(value).matches()) {
			return true;
		}
		return false;
	}
	/**
	 * 验证手机号
	 * @param value
	 * @return
	 */
	public static boolean isMphone(String value){
		if(StringUtils.isBlank(value))
			return false;
		Pattern pattern = Pattern.compile("^1[3-8]+\\d{9}$");
		if (pattern.matcher(value).matches()) {
			return true;
		}
		return false;
	}
	/**
	 * 银行卡号验证
	 * @param value
	 * @return
	 */
	public static boolean isbankcardid(String value){
		if(StringUtils.isBlank(value)){
			return false;
		}
		Pattern pattern = Pattern.compile("^(\\d{16}|\\d{19})$");
		if (pattern.matcher(value).matches()) {
			return true;
		}
		return false;
	}
	/**
	 * 获取 order by 开始位置
	 * 
	 * @param orderbysql
	 * @return
	 */
	public static int getOrderByIndex(String orderbysql) {
		int index = -1;
		if (StringUtils.isBlank(orderbysql)) {
			return index;
		}
		String regStr = "\\s+(order)\\s+(by)";
		Pattern pattern = Pattern.compile(regStr);
		Matcher matcher = pattern.matcher(orderbysql.toLowerCase());
		if (matcher.find()) {
			index = matcher.start();
		}
		return index;
	}

	/**
	 * 验证是否是身份证号
	 * 
	 * @param value
	 * @return
	 */
	public static boolean isCardid(String value){
		if(StringUtils.isBlank(value)){
			return false;
		}
		Map<Integer,String> city = new HashMap<Integer, String>();
		city.put(11,"北京");
		city.put(12,"天津");
		city.put(13,"河北");
		city.put(14,"山西");
		city.put(15,"内蒙古");
		city.put(21,"辽宁");
		city.put(22,"吉林");
		city.put(23,"黑龙江 ");
		city.put(31,"上海");
		city.put(32,"江苏");
		city.put(33,"浙江");
		city.put(34,"安徽");
		city.put(35,"福建");
		city.put(36,"江西");
		city.put(37,"山东");
		city.put(41,"河南");
		city.put(42,"湖北 ");
		city.put(43,"湖南");
		city.put(44,"广东");
		city.put(45,"广西");
		city.put(46,"海南");
		city.put(50,"重庆");
		city.put(51,"四川");
		city.put(52,"贵州");
		city.put(53,"云南");
		city.put(54,"西藏 ");
		city.put(61,"陕西");
		city.put(62,"甘肃");
		city.put(63,"青海");
		city.put(64,"宁夏");
		city.put(65,"新疆");
		city.put(71,"台湾");
		city.put(81,"香港");
		city.put(82,"澳门");
		city.put(91,"国外 ");
		//String tip = "";
		boolean pass= true;
		Pattern pattern = Pattern.compile("^\\d{15}|^\\d{17}([0-9]|X|x)$");
		if(StringUtils.isBlank(value) || !pattern.matcher(value).matches()){
            //tip = "身份证号格式错误";
            pass = false;
        }else if (city.get(Integer.parseInt(value.substring(0,2)))==null){
        	//tip = "地址编码错误";
            pass = false;
        }else{
        	//18位身份证需要验证最后一位校验位
            if(value.length() != 18){
            	return pass;
            }
            String [] factor = {"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"};
            String [] parity = {"1", "0","X", "9", "8", "7", "6", "5", "4", "3", "2"};
            int sum = 0;
            int ai = 0;
            int wi = 0;
            for (int i = 0; i < 17; i++)
            {
                ai = Integer.parseInt(value.substring(i, i+1));
                wi = Integer.parseInt(factor[i]);
                sum += ai * wi;
            }
            String last = parity[sum % 11];
            if(!last.equals(value.substring(17))){
                //tip = "校验位错误";
                pass =false;
            }
        }
		return pass;
	}
}
